/**
 * 
 */
package org.gbif.agi.dao.hibernate;

import java.util.List;

import org.appfuse.dao.hibernate.GenericDaoHibernate;
import org.gbif.agi.dao.GenusDao;
import org.gbif.agi.model.Genus;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * @author timrobertson
 */
public class GenusDaoHibernate extends GenericDaoHibernate<Genus, Long> implements GenusDao {
	public GenusDaoHibernate() {
		super(Genus.class);
	}

	/**
	 * @see org.gbif.agi.dao.GenusDao#findByStartingWith(java.lang.String, int)
	 */
	@SuppressWarnings("unchecked")
	public List<Genus> findByStartingWith(final String start, final int maxResults) {
		return (List<Genus>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session) {
						Query query = 
							session.createQuery("from Genus where fullScientificName like :start group by canonical order by canonical")
								.setString("start", start + "%")
								.setMaxResults(maxResults);
						return query.list();
					}
				}
			);		
	}
}